#include "TwoTimersDialog.h"
#include <QVBoxLayout>
#include <QMessageBox>
#include <QDebug>

TwoTimersDialog::TwoTimersDialog(QWidget *parent) :
    QDialog(parent)
{
    m_labelOne=new QLabel("timer1 :0", this);
    m_labelTwo=new QLabel("timer2 :0", this);
    m_setButton = new QPushButton("set", this);
    m_stopButton = new QPushButton("stop", this);
    m_timerValue = new QLineEdit("300", this);
    m_comboBox = new QComboBox(this);
    m_comboBox->addItem("timer1");
    m_comboBox->addItem("timer2");

    setLayout(new QVBoxLayout);

    layout()->addWidget(m_labelOne);
    layout()->addWidget(m_labelTwo);
    layout()->addWidget(m_comboBox);
    layout()->addWidget(m_timerValue);
    layout()->addWidget(m_setButton);
    layout()->addWidget(m_stopButton);

    connect(m_setButton, SIGNAL(clicked()), this, SLOT(setTimer()));
    connect(m_stopButton, SIGNAL(clicked()), this, SLOT(stopTimer()));

    m_timerId1 =0;
    m_timerId2 =0;
}

void TwoTimersDialog::setTimer()
{
    if (QString::compare(m_comboBox->currentText(), "timer1")==0)
    {
        killTimer(m_timerId1);
        m_timerId1 = startTimer(m_timerValue->text().toInt());
    }
    else if (QString::compare(m_comboBox->currentText(), "timer2")==0)
    {
        killTimer(m_timerId2);
        m_timerId2 = startTimer(m_timerValue->text().toInt());
    }
}

void TwoTimersDialog::stopTimer()
{
    if (QString::compare(m_comboBox->currentText(), "timer1")==0)
    {
        killTimer(m_timerId1);
    }
    else if (QString::compare(m_comboBox->currentText(), "timer2")==0)
    {
        killTimer(m_timerId2);
    }
}
void TwoTimersDialog::timerEvent(QTimerEvent *)
{
    static int counter1=0;
    static int counter2=0;
    if (QString::compare(m_comboBox->currentText(), "timer1")==0)
    {
        m_labelOne->setText(QString("timer1 : ") + QString::number(++counter1));
    }
    else if (QString::compare(m_comboBox->currentText(), "timer2")==0)
    {
        m_labelTwo->setText(QString("timer2 : ") + QString::number(++counter2));
    }
}

